import router from "@/router";
import store from "@/store";
import nprogress from "nprogress";
import "nprogress/nprogress.css";

const whiteList = ["/login", "/404"];
//全局前置路由守卫
router.beforeEach(async (to, from, next) => {
  nprogress.start();
  const { token } = store.getters;
  if (token) {
    //如果有token 并且去的是登录页，就跳到主页
    if (to.path === "/login") {
      next("/");
      nprogress.done();
    } else {
      if (!store.getters.userid) {
        await store.dispatch('user/getUserInfo')
        }
      next();
    }
  } else {
    //如果没有token
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next("/login");
      nprogress.done();
    }
  }
});
router.afterEach(() => {
  //关闭进度条
  nprogress.done();
});